package com.gpj.learn.rabbitmq.springboot.system;

import lombok.AllArgsConstructor;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.FactoryBean;

/**
 * @className BindingFactoryBean
 * @description bind的BeanFactory
 * @author GPJ
 * @date 2021/9/3 16:09
 * @version 1.0
 **/
@AllArgsConstructor
public class BindingFactoryBean implements FactoryBean {
	private Queue queue;
	private Exchange exchange;
	private String routingKey;

	@Override
	public Object getObject() throws Exception {
		if (exchange instanceof DirectExchange) { // 路由模式绑定
			return BindingBuilder.bind(queue).to((DirectExchange) exchange).with(routingKey);
		} else if (exchange instanceof TopicExchange) { // 主题模式绑定
			return BindingBuilder.bind(queue).to((TopicExchange) exchange).with(routingKey);
		} else if (exchange instanceof FanoutExchange) { // 广播模式绑定
			return BindingBuilder.bind(queue).to((FanoutExchange) exchange);
		} else {
			throw new RuntimeException("未知的RabbitMQ模式绑定,请检查MQ相关配置是否正确 -> " +
					"{queue:" + queue.getName() + ", exchange:" + exchange.getName() + ", routingKey:" + routingKey + "}");
		}
	}

	@Override
	public Class<?> getObjectType() {
		return Binding.class;
	}
}
